//
//  ArtTour.m
//  SmartRoadShow
//
//  Created by  ibokan on 10-9-25.
//  Copyright 2010 simonwang142232@gmail.com. All rights reserved.
//

#import "ArtTour.h"
#import "database.h"
#import "NSDate-Helper.h"
#import "DateUtil.h"

@implementation ArtTour
@synthesize _id,_city,_starttime,_endtime,_location;
@synthesize _address,_mapURL,_thumb,_datepic;


+(NSArray *) searchAllArtTour:(NSString *)dateString
{
	PLSqliteDatabase *db = [database setup];
	id<PLResultSet> rs;
	
	NSMutableArray *artTourArray = [[NSMutableArray alloc] init];
	rs = [db executeQuery:@"select * from ArtTour"];
	while ([rs next])
	{
		int ID = [[rs objectForColumn:@"ID"] intValue];
		NSString *city = [rs objectForColumn:@"city"];
		NSString *starttime = [rs objectForColumn:@"starttime"];
		NSString *endtime = [rs objectForColumn:@"endtime"];
		NSString *location = [rs objectForColumn:@"location"];
		NSString *address = [rs objectForColumn:@"address"];
		NSString *mapURL = [rs objectForColumn:@"mapURL"];
		NSString *thumb = [rs objectForColumn:@"thumb"];
		NSString *datepic = [rs objectForColumn:@"datepic"];
		
		if([DateUtil diffBetweenDateStr:dateString andDateStr:starttime] && [DateUtil diffBetweenDateStr:endtime andDateStr:dateString])
		{

			ArtTour *artTour = [[ArtTour alloc] initWithID:ID
													  City:city
												 Starttime:starttime
												   Endtime:endtime
												  Location:location
												   Address:address
													 Thumb:thumb
													MapURL:mapURL
												   Datepic:datepic];
			[artTourArray addObject:artTour];
			[artTour release];
		}
	}
	[rs close];

	return [artTourArray autorelease];
}

+(BOOL) IsExistArtTour:(NSString *)dateString
{
	PLSqliteDatabase *db = [database setup];
	id<PLResultSet> rs;
	
	rs = [db executeQuery:@"select starttime,endtime from ArtTour"];
	while ([rs next])
	{
		NSString *start = [NSString stringWithFormat:@"%@ 00:00:00",[rs objectForColumn:@"starttime"] ];
		NSString *end  = [NSString stringWithFormat:@"%@ 00:00:00",[rs objectForColumn:@"endtime"] ];		
		if([DateUtil diffBetweenDateStr:dateString andDateStr:start] && [DateUtil diffBetweenDateStr:end andDateStr:dateString])
		{
			return YES;
		}
	}
	return NO;
}

//+(BOOL)findArtTourId:(int)artId
//{
//	PLSqliteDatabase *db = [database setup];
//	id<PLResultSet> rs;
//	
//	rs = [db executeQuery:@"select artTour_id from ArtTour"];
//	while ([rs next])
//	{
//		int artTourId = [[rs objectForColumn:@"artTour_id"] intValue];
//		if(artId == artTourId)
//		{
//			return YES;
//		}
//	}
//	return NO;
//}


+(NSArray *) findAll
{
	PLSqliteDatabase *db = [database setup];
	id<PLResultSet> rs;
	
	rs = [db executeQuery:@"select * from ArtTour"];
	NSMutableArray *artTourArray = [[NSMutableArray alloc] init];
	while ([rs next])
	{
		int ID = [[rs objectForColumn:@"ID"] intValue];
		NSString *city = [rs objectForColumn:@"city"];
		NSString *starttime = [rs objectForColumn:@"starttime"];
		NSString *endtime = [rs objectForColumn:@"endtime"];
		NSString *location = [rs objectForColumn:@"location"];
		NSString *address = [rs objectForColumn:@"address"];
		NSString *thumb = [rs objectForColumn:@"thumb"];
		NSString *mapURL = [rs objectForColumn:@"mapURL"];
		NSString *datepic = [rs objectForColumn:@"datepic"];
		
	    ArtTour *artTour = [[ArtTour alloc] initWithID:ID
												  City:city
											 Starttime:starttime
											   Endtime:endtime
											  Location:location
											   Address:address
												 Thumb:thumb
												MapURL:mapURL
											   Datepic:datepic];
		
		[artTourArray addObject:artTour];
		[artTour release];
	}
	
	[rs close];
	
	return [artTourArray autorelease];
}


+ (int) insertWithID:(int)Id City:(NSString *) city Starttime:(NSString *)start Endtime:(NSString *)end
			Location:(NSString *)loca Thumb:(NSString *)thmb Address:(NSString *)addrs MapURL:(NSString *) mapUrl Datepic:(NSString *) dareP{
	PLSqliteDatabase *db = [database setup];
	
	BOOL re =[db executeUpdate:@"insert into ArtTour(city,starttime,endtime,location,address,thumb,mapURL,datepic) values(?,?,?,?,?,?,?,?)",
			  city,
			  start,
			  end,
			  loca,
			  addrs,
			  thmb,
			  mapUrl,
			  dareP];
	
    return re;
}

+(int)deleteAll{
	PLSqliteDatabase *db = [database setup];
	
	BOOL re = [db executeUpdate:@"delete from ArtTour"];
	
	return re;
}

+(int)delete:(int)Id
{
	PLSqliteDatabase *db = [database setup];
	
	BOOL re = [db executeUpdate:@"delete from ArtTour where ID = ?",[NSNumber numberWithInt:Id]];
	
	return re;
}

-(id) initWithID:(int)Id City:(NSString *)city Starttime:(NSString *)start Endtime:(NSString *)end
		Location:(NSString *)loca Address:(NSString *)addr Thumb:(NSString *) thmb MapURL:(NSString *) mapUrl Datepic:(NSString *)dateP;
{
	if(self = [super init])
	{
		_id = Id;
		_city = [city retain];
		_starttime = [start retain];
		_endtime = [end retain];
		_location = [loca retain];
		_address = [addr retain];
		_thumb = [thmb retain];
		_mapURL = [mapUrl retain];
		_datepic = [dateP retain];
	}
	
	return self;
}

-(int) Id
{
	return _id;
}


-(NSString *)city
{
	return _city;
}

-(NSString *) starttime
{
	return _starttime;
}

-(NSString *)endtime
{
	return _endtime;
}

-(NSString *)location
{
	return _location;
}

-(NSString *) address
{
	return _address;
}

- (NSString *) thumb{
	return _thumb;
}

- (NSString *) mapURL{
	return _mapURL;
}

- (NSString *) datepic{
	return _datepic;
}

@end
